package com;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class JDBC_test_base_DML {

	public static void main(String[] args) throws Exception {
		/**
		 * 增加
		 */
		Connection connection = DBUtil.getConnection();
		String sql = "INSERT INTO test_jdbc (id,name,money)VALUES(5,'啊啊',33.22)";
		PreparedStatement preparedStatement = connection.prepareStatement(sql);
		int count = preparedStatement.executeUpdate(sql);
		System.out.println(count);
		DBUtil.close(preparedStatement);
		DBUtil.close(connection);

		/**
		 * 删除
		 */
		Connection connection1 = DBUtil.getConnection();
		String sql1 = "delete from test_jdbc where id = 1";
		PreparedStatement preparedStatement1 = connection1.prepareStatement(sql1);
		int count1 = preparedStatement1.executeUpdate();
		System.out.println(count1);
		DBUtil.close(preparedStatement1);
		DBUtil.close(connection1);

		/**
		 * 更改
		 */
		Connection connection2 = DBUtil.getConnection();
		String sql2 = "UPDATE test_jdbc set name = '123' WHERE name = '王五'";
		PreparedStatement preparedStatement2 = connection2.prepareStatement(sql2);
		int count2 = preparedStatement2.executeUpdate();
		System.out.println(count2);
		DBUtil.close(preparedStatement2);
		DBUtil.close(connection2);

		/**
		 * 查找
		 */
		Connection connection3 = DBUtil.getConnection();
		String sql3 = "select * from  test_jdbc ";
		PreparedStatement preparedStatement3 = connection3.prepareStatement(sql3);
		ResultSet resultSet = preparedStatement3.executeQuery();
		while (resultSet.next()) {
			System.out.print(resultSet.getInt("id") + " ");
			System.out.print(resultSet.getString("name") + " ");
			System.out.println(resultSet.getDouble("money") + " ");

		}
		DBUtil.close(preparedStatement3);
		DBUtil.close(connection3);
		DBUtil.close(resultSet);
	}
}
